package xyz.xtt.asset.service.impl;

import static xyz.xtt.common.asset.AssertUtils.isTrue;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.google.common.primitives.Ints;

import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.constants.ErrorMessage;
import xyz.xtt.asset.service.INftService;
import xyz.xtt.asset.vo.DeilverCommonResponse;
import xyz.xtt.asset.vo.TransferFromResult;

@Slf4j
public class ElementHandler {
	private Long perNum;
	private int arrayLength = 0;
	private StringRedisTemplate stringRedisTemplate;
	private INftService nftService;

	public ElementHandler(StringRedisTemplate stringRedisTemplate, INftService nftService, Long perNum, int length) {
		this.stringRedisTemplate = Objects.requireNonNull(stringRedisTemplate, "stringRedisTemplate is null");
		this.nftService = Objects.requireNonNull(nftService, "nftService is null");
		this.perNum = Objects.requireNonNull(perNum, "perNum is null");
		this.arrayLength = length;
	}

	private Long requestCount = 0L;
//	private int currentElementIndex = 0;

	private String requestCountkey() {
		return "nft:element:requestCount";
	}

	private String currentIndex() {
		return "nft:element:currentIndex";
	}

	private int getCurrentIndex() {
		String val = stringRedisTemplate.opsForValue().get(currentIndex());
		if (StringUtils.isBlank(val)) {
			return 0;
		}
		return Ints.tryParse(val);
	}

	private Long getRequestCount() {
		String key = requestCountkey();
		Long oldVlaue = stringRedisTemplate.opsForValue().increment(key, 0);
		stringRedisTemplate.opsForValue().increment(key, 1);
		return oldVlaue;
	}

	public void next() {
		int currentElementIndex = getCurrentIndex();
		requestCount = stringRedisTemplate.opsForValue().increment(requestCountkey(), 1);
		// 在达到10次请求后更新当前元素索引
		if (requestCount % perNum == 0) {
			currentElementIndex = (currentElementIndex + 1) % arrayLength;
			stringRedisTemplate.opsForValue().set(currentIndex(), String.valueOf(currentElementIndex));
		}
	}

	public DeilverCommonResponse send(Long elementEntitlementId, String orderNo, Long[] elementAccountNo, Long ttUserId, String bizType) {
		int currentElementIndex = getCurrentIndex();
		int index = currentElementIndex;
		log.info("please element config currentElementIndex :{},elementAccountNo.length:{} orderNo:{}", currentElementIndex, elementAccountNo.length, orderNo);
		isTrue(currentElementIndex >= elementAccountNo.length, ErrorMessage.UNKNOWN_ERROR);
		Long accountNo = elementAccountNo[currentElementIndex];
		TransferFromResult transferFromWithActivity = nftService.transferFromWithActivity(accountNo, ttUserId, orderNo, 1, elementEntitlementId, bizType);
		requestCount = stringRedisTemplate.opsForValue().increment(requestCountkey(), 1);
		// 在达到10次请求后更新当前元素索引
		if (requestCount % perNum == 0) {
			currentElementIndex = (currentElementIndex + 1) % arrayLength;
			stringRedisTemplate.opsForValue().set(currentIndex(), String.valueOf(currentElementIndex));
		}
		return DeilverCommonResponse.buildResult(transferFromWithActivity, null, index);
	}

}
